package com.pj.wisdomlogistics.utils;


import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.other.EventCenter;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarUtil {

    public Calendar cal;
    public String selectMonth;
    public String selectDay;
    public String selectHour;
    public String selectMinute;

    public SimpleDateFormat mSf = new SimpleDateFormat("yyyy-MM-dd");

    private CalendarUtil() {
        cal = Calendar.getInstance();

    }

    private static class ManagerHolder {
        private static final CalendarUtil instance = new CalendarUtil();
    }

    public static CalendarUtil create() {
        return ManagerHolder.instance;
    }

    public void setCalendarDate(Context mContext, final TextView mTextView) {
        DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                selectMonth  = (month + 1) >= 10 ? (month + 1) + "" : "0" + (month + 1);
                selectDay = dayOfMonth >= 10 ? dayOfMonth + "" : "0" + dayOfMonth;
                mTextView.setText(year + "-" + selectMonth + "-" + selectDay);
            }
        }, cal.get(Calendar.YEAR) , cal.get(Calendar.MONTH) , cal.get(Calendar.DAY_OF_MONTH));
        mDatePickerDialog.show();
    }

    public void setCalendarTime(Context mContext , final TextView mTextView){
        TimePickerDialog mTimePickerDialog_start = new TimePickerDialog(mContext, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                selectHour = hourOfDay >= 10 ? hourOfDay + "" : "0" + hourOfDay;
                selectMinute = minute >= 10 ? minute + "" : "0" + minute;
                mTextView.setText(selectHour + ":" + selectMinute);
            }
        } , Calendar.HOUR_OF_DAY , Calendar.MINUTE , true);
        mTimePickerDialog_start.show();
    }

    public void getCalendarDate(final Context mContext) {
        DatePickerDialog mDatePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                selectMonth  = (month + 1) >= 10 ? (month + 1) + "" : "0" + (month + 1);
                selectDay = dayOfMonth >= 10 ? dayOfMonth + "" : "0" + dayOfMonth;
                String date = year + "-" + selectMonth + "-" + selectDay;
                    EventBus.getDefault().post(new EventCenter<>(Cons.Date.DATE , date));

            }
        }, cal.get(Calendar.YEAR) , cal.get(Calendar.MONTH) , cal.get(Calendar.DAY_OF_MONTH));
        mDatePickerDialog.show();
    }



}
